package com.hc.config.satoken.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * 路由拦截鉴权
 */
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("classpath:/img/")
        .addResourceLocations("classpath:/static/");
    }



    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> {
            // 排除掉的 path 列表，可以写多个
            SaRouter.match("/**").notMatch("/user/login","/druid/**",
                    "/swagger/**","/swagger-ui/**","/userAvatar/**","/img/**","/css/**",
                    "/user/register","/user/sendEmailCode","/swagger-ui.html",
                    "/v3/**","/v2/**","/swagger-resources/**","/webjars/**","/doc.html"
             //处理与请求，不参与权限和登录校验
            ).notMatch(SaHttpMethod.OPTIONS).check(r-> StpUtil.checkLogin());
        }));
    }
}
